package www.openteam.com.model.policy;

import android.util.Log;

import java.util.ArrayList;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.ApprlSuccess;
import www.openteam.com.bean.OutFactoryMsg;
import www.openteam.com.bean.SendRcvPoductMsg;
import www.openteam.com.bean.SendRcvUserMsg;
import www.openteam.com.model.PrdctnMngmnt.SendRCKModel;
import www.openteam.com.model.PrdctnMngmnt.SendRCkSearchModel;

/**
 * Created by Administrator on 2019-06-21.
 */

public class SendRcvApprlModel extends BaseModel {

    private String TAG = getClass().getSimpleName();

    public void findUserMsg(String rdsno, String startDate, String endDate, String status,
                            final UserMsgListener listener) {
        httpApiService.findUserMsg(rdsno, startDate, endDate, status).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<SendRcvUserMsg>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.fail(e);
                    }

                    @Override
                    public void onNext(SendRcvUserMsg data) {
                        listener.success(data);
                    }
                })
        ;
    }

    public void findPoductMsg(ArrayList<String> ids, final PoductMsgListener listener) {

//        ArrayList<String> ids = new ArrayList<>();
//       int i =  0;
//       if (i>=ids.size()){
//           findPoductMsg();
//       }
        Log.e(TAG, "findPoductMsg: "+ids.size() );
        Observable.from(ids).concatMap(new Func1<String, Observable<SendRcvPoductMsg>>() {
            @Override
            public Observable<SendRcvPoductMsg> call(String s) {
//                Log.e(TAG, "call: "+s );
                return httpApiService.findPoductMsg(s);
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<SendRcvPoductMsg>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.fail(e);
                    }

                    @Override
                    public void onNext(SendRcvPoductMsg data) {
                        listener.success(data);
                    }
                });


//                        httpApiService.findPoductMsg(id).subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<SendRcvPoductMsg>() {
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        listener.fail(e);
//                    }
//
//                    @Override
//                    public void onNext(SendRcvPoductMsg data) {
//                        listener.success(data);
//                    }
//                })
//        ;
    }

    public void audit(String checker, String checkDate, String id, final ApprlListener listener) {
        httpApiService.audit(checker, checkDate, id).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ApprlSuccess>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.fail(e);
                    }

                    @Override
                    public void onNext(ApprlSuccess data) {
                        listener.success(data);
                    }
                })
        ;
    }

    public void reverseAudit(String id, final ApprlListener listener) {
        httpApiService.reverseAudit(id).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ApprlSuccess>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.fail(e);
                    }

                    @Override
                    public void onNext(ApprlSuccess data) {
                        listener.success(data);
                    }
                })
        ;
    }

    public interface UserMsgListener {
        void success(SendRcvUserMsg sendRcvUserMsg);

        void fail(Throwable e);
    }

    public interface PoductMsgListener {
        void success(SendRcvPoductMsg sendRcvPoductMsg);

        void fail(Throwable e);
    }

    public interface ApprlListener {
        void success(ApprlSuccess apprlSuccess);

        void fail(Throwable e);
    }

    public interface ReverseAuditListener {
        void success(ApprlSuccess ApprlSuccess);

        void fail(Throwable e);
    }
}
